﻿Blazor.registerFunction('log', function (message) {
    console.log(message);
});
Blazor.registerFunction('toastr', function (toastType, message) {
    toastr.error(message)
});
//设置头部状态
Blazor.registerFunction('setHeaderStatus', () => {

    if (localStorage.getItem('token') === null || localStorage.getItem('token')==="") {
        document.getElementById("unlogin").setAttribute("style", "");       
        document.getElementById("logined").setAttribute("style", "display:none");  
    } else {
        document.getElementById("unlogin").setAttribute("style", "display:none");  
        document.getElementById("logined").setAttribute("style", "");
        document.getElementById("spanUsername").innerText=localStorage.getItem('username');
    }

});

Blazor.registerFunction('reload', () => {
    location.reload();

});
Blazor.registerFunction('goHome', () => {
    location.href="/";

});


//元素焦点
Blazor.registerFunction('BlazorFocus.FocusElement', function (element) {
    element.focus();
});

//设置令牌
Blazor.registerFunction('Auth.SetToken', function (token,username) {
     
        localStorage.setItem("token", token);
        localStorage.setItem("username", username);       

   // return true;
});

//获取令牌
Blazor.registerFunction('Auth.GetToken', function () {

        if (localStorage.getItem('token') === null) {
            return "";
        }
        return localStorage.getItem("token");

});

//获取令牌
Blazor.registerFunction('Auth.GetUsername', function () {    
    
    if (localStorage.getItem('username') === null) {
        return "";
    }
    return localStorage.getItem("username"); 
});

//清除令牌
Blazor.registerFunction('Auth.RemoveToken', function () {
    // Check browser support
    if (typeof (Storage) !== "undefined") {
       // localStorage.removeItem("token");
        localStorage.setItem("username", "");
        localStorage.setItem("token", "");
    } else {
        alert("抱歉！您的浏览器不支持 Web Storage ...");
        return "";
    }
});